<?php

	class LoginController extends CommonController{

		public $model;

		public function __init(){
			parent:: __init();

			$this->model = K('Shop_user');
		}

		public function index(){

			$this->dis('login');
		}

		public function code(){
			$code = new Code();
			$code ->show();
		}

		public function login(){
			if (IS_POST) {
				$username= Q('username');
				$password = md5(Q('password'));
				$user = $this->model->where("username='{$username}' && password='{$password}'")->find();
				if (empty($user)) {
					$this->error('用户不存在');
				}else{
					$_SESSION['uid']=$user['uid'];
					$_SESSION['username']=$user['username'];

				if (isset($_POST['auto'])) {
					setcookie(session_name(),session_id(),time()+3600*24*7,'/');
				}else{
					setcookie(session_name(),session_id(),0,'/');
				}


					$this->success('成功登录',__ROOT__);
				}
			}
		}

		public function validate_username(){
			$username = Q('post.username');
			$userInfo = $this->model->where("username='{$username}'")->find();
			// p($userInfo);die;
			if ($userInfo) {
				$arr = array('status'=>1,'message'=>'正确');
			}else{
				$arr = array('status'=>0,'message'=>'用户名不存在');
			}

			$this->ajax($arr);

		}

		public function validate_code(){
			$code = strtoupper(Q('post.code'));
			$pregCode = strtoupper($_SESSION['code']);
			if ($code != $pregCode) {
				$arr = array('status'=>0,'message'=>'验证码不正确');
			}else{
				$arr = array('status'=>1,'message'=>'正确');
			}

			$this->ajax($arr);

		}

		public function out(){
			//清除session
			session(NULL);
			go(U('Index/index'));
		}

		
	}





?>